import { createApp } from 'vue'
import App from './App.vue'
import { createRouter, createWebHistory } from 'vue-router'
import { routes } from './router'

if ((window as any).__POWERED_BY_QIANKUN__) {
  __webpack_public_path__ = (window as any).__INJECTED_PUBLIC_PATH_BY_QIANKUN__;
}

let router: any
let app: any

function render({ container } = {} as any) {
  router = createRouter({
    history: createWebHistory((window as any).__POWERED_BY_QIANKUN__ ? '/child2' : '/'),
    routes,
  })

  app = createApp(App)
  app.use(router).mount(container ? container.querySelector('#app') : '#app')
}


if(!(window as any).__POWERED_BY_QIANKUN__) {
  render()
}

// 导出第一次进入当前子应用的钩子函数
export async function bootstrap() {
}

// 导出每次创建挂载时的钩子函数
export async function mount(props: any) {
  render(props)
}

// 导出每次销毁时的钩子函数
export async function unmount(props: any) {
  app.unmount()
  app._container.innerHTML = ''
  app = null
  router = null
}
